//
//  JBUnitView.m
//  JBCalendar
//
//  Created by YongbinZhang on 7/8/13.
//  Copyright (c) 2013 JustBen. All rights reserved.
//

#import "JBUnitView.h"

@interface JBUnitView ()

@property (nonatomic, strong) JBUnitGridView *previousUnitGridView;
@property (nonatomic, strong) JBUnitGridView *selectedUnitGridView;
@property (nonatomic, strong) JBUnitGridView *followingUnitGridView;

@property (nonatomic, strong) UISwipeGestureRecognizer *swipeLeftGR;
- (void)selectorForSwipeLeftGR:(UISwipeGestureRecognizer *)swipeLeftGR;
@property (nonatomic, strong) UISwipeGestureRecognizer *swipeRightGR;
- (void)selectorForSwipeRightGR:(UISwipeGestureRecognizer *)swipeRightGR;

- (void)test:(UITapGestureRecognizer *)tap;

@end

@implementation JBUnitView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/**************************************************************
 *@Description:初始化方法
 *@Params:
 *  frame:框架
 *  unitType:月视图／周视图
 *  selectedDate:初始的选择日期
 *@Return:JBUnitGridView对象
 **************************************************************/
- (id)initWithFrame:(CGRect)frame UnitType:(UnitType)unitType SelectedDate:(NSDate *)selectedDate
{
    self = [self initWithFrame:frame];
    if (self) {
        self.selectedDate = selectedDate;
        
        self.selectedUnitGridView = [[JBUnitGridView alloc] initWithFrame:self.bounds UnitType:unitType SelectedDate:self.selectedDate];

        self.previousUnitGridView = [[JBUnitGridView alloc] initWithFrame:self.bounds UnitType:unitType SelectedDate:[self.selectedDate associateDayOfThePreviousMonth]];
        self.previousUnitGridView.hidden = YES;

        self.followingUnitGridView = [[JBUnitGridView alloc] initWithFrame:self.bounds UnitType:unitType SelectedDate:[self.selectedDate associateDayOfTheFollowingMonth]];
        self.followingUnitGridView.hidden = YES;
        
        
        self.calendarView = [[UIView alloc] initWithFrame:self.selectedUnitGridView.bounds];
        self.calendarView.backgroundColor = [UIColor lightGrayColor];
        
        [self.calendarView addSubview:self.previousUnitGridView];
        [self.calendarView addSubview:self.followingUnitGridView];
        [self.calendarView addSubview:self.selectedUnitGridView];
        [self.calendarView bringSubviewToFront:self.selectedUnitGridView];
        [self addSubview:self.calendarView];
        
        self.swipeLeftGR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(selectorForSwipeLeftGR:)];
        self.swipeLeftGR.direction = UISwipeGestureRecognizerDirectionLeft;
        [self.calendarView addGestureRecognizer:self.swipeLeftGR];
        self.swipeRightGR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(selectorForSwipeRightGR:)];
        self.swipeRightGR.direction = UISwipeGestureRecognizerDirectionRight;
        [self.calendarView addGestureRecognizer:self.swipeRightGR];
    }
    
    return self;
}

#pragma mark -
#pragma mark - Class Extensions
- (void)selectorForSwipeLeftGR:(UISwipeGestureRecognizer *)swipeLeftGR
{
    self.selectedDate = [self.selectedDate associateDayOfTheFollowingMonth];
//    self.selectedUnitGridView.selectedDate = self.selectedDate;
    
    self.previousUnitGridView.hidden = YES;
    self.followingUnitGridView.hidden = NO;
    [UIView animateWithDuration:0.2f animations:^{
        CGRect tmpFrame = self.selectedUnitGridView.frame;
        tmpFrame.origin.y = self.selectedUnitGridView.bounds.size.height;
        self.selectedUnitGridView.frame = tmpFrame;
        
        self.calendarView.frame = self.followingUnitGridView.frame;
    }];
    self.selectedUnitGridView.hidden = YES;
    CGRect tmpFrame = self.selectedUnitGridView.frame;
    tmpFrame.origin.y = 0.0f;
    self.selectedUnitGridView.frame = tmpFrame;
    
    JBUnitGridView *tmpGridView = self.previousUnitGridView;
    self.previousUnitGridView = self.selectedUnitGridView;
    self.selectedUnitGridView = self.followingUnitGridView;
    [self.calendarView bringSubviewToFront:self.selectedUnitGridView];

    self.followingUnitGridView = tmpGridView;
    self.followingUnitGridView.selectedDate = [self.selectedDate associateDayOfTheFollowingMonth];
}

- (void)selectorForSwipeRightGR:(UISwipeGestureRecognizer *)swipeRightGR
{
    self.selectedDate = [self.selectedDate associateDayOfThePreviousMonth];
//    self.selectedUnitGridView.selectedDate = self.selectedDate;
    
    self.followingUnitGridView.hidden = YES;
    self.previousUnitGridView.hidden = NO;
    [UIView animateWithDuration:0.2f animations:^{
        CGRect tmpFrame = self.selectedUnitGridView.frame;
        tmpFrame.origin.y = 0.0f - self.selectedUnitGridView.bounds.size.width;
        self.selectedUnitGridView.frame = tmpFrame;
        
        self.calendarView.frame = self.previousUnitGridView.frame;
    }];
    self.selectedUnitGridView.hidden = YES;
    CGRect tmpFrame = self.selectedUnitGridView.frame;
    tmpFrame.origin.y = 0.0f;
    self.selectedUnitGridView.frame = tmpFrame;
    
    JBUnitGridView *tmpGridView = self.followingUnitGridView;
    self.followingUnitGridView = self.selectedUnitGridView;
    self.selectedUnitGridView = self.previousUnitGridView;
    [self.calendarView bringSubviewToFront:self.selectedUnitGridView];

    self.previousUnitGridView = tmpGridView;
    self.previousUnitGridView.selectedDate = [self.selectedDate associateDayOfThePreviousMonth];
}

@end
